package com.thti.mecool.client.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.thti.mecool.client.adapter.MakePlanSelectTaskAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.model.biz.ProjectServerBizImpl;
import com.thti.mecool.client.model.po.ProjectServer;
import com.thti.mecool.client.widget.Toolbar;

public class MakePlanSelectTaskActivity extends BaseActivity {
	ListView listView;
	private MakePlanSelectTaskAdapter adapter;
	List<ProjectServer> projectServerList;
private String projectId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.noTitle();
		setContentView(R.layout.mecool_make_plan_select_task);
		initToolbar();
		Cache.add(this);
		projectId=getIntent().getStringExtra("projectId");
		listView = (ListView) findViewById(R.id.mecool_make_plan_select_task_listview);
		initSql();
	}

	private void initSql() {
		ProjectServerBizImpl projectServerBizImpl = new ProjectServerBizImpl(
				this);
		projectServerList = projectServerBizImpl.findBystore(projectId,1);
		service();
		
	}

	private void service() {
		if (adapter == null) {
			adapter = new MakePlanSelectTaskAdapter(this, projectServerList);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(listListener);
		} else {
			adapter.notifyDataSetChanged();
		}
	}

	OnItemClickListener listListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub

			Intent i = new Intent();
			Bundle data = new Bundle();
			// data.puts
			data.putString("TaskName", projectServerList.get(arg2).getName());
			data.putString("TaskId", projectServerList.get(arg2).getTaskId());
			i.putExtras(data);
			setResult(RESULT_OK, i);
			finish();
		}
	};

	public void initToolbar() {
		Toolbar toolbar = new Toolbar(this);
		toolbar.setToolbarCentreText("制定计划-选择任务");
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
	}

	private OnClickListener leftbuttonListener = new ImageButton.OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};

}
